import { Transaction } from "tiny-entity2";
import { IQueryObject } from "tiny-entity2/queryObject";
import { ApiDataContext } from "../domain/apiDataContext";
import { AccountPermits } from "../domain/models/accountPermits";
import { OrganizationTree } from "../domain/models/organizationTree";
import { BaseService } from "./baseService";

export class OrganizationTreeService extends BaseService {
    async CreateorganizationTree(entity: OrganizationTree, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new OrganizationTree();
            d.ConverToEntity(entity);
            return ctx.Create(d);
        });
    }
    async UpdateorganizationTree(entity: OrganizationTree, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new OrganizationTree();
            d.ConverToEntity(entity);
            return ctx.Update(d);
        });
    }
    async DeleteOrganizationTree(entity: OrganizationTree, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new OrganizationTree();
            d.ConverToEntity(entity);
            // ctx.Delete<OrganizationTree>(x=>x.name == $args1, ctx.User,  {$args1: 'ddddd'});
            return ctx.Delete(d);
        });
    }
    async GetOrganizationTreeById(id: string) {
        let ctx = new ApiDataContext();
        let r = await ctx.OrganizationTree.First(x => x.id == $args1, { $args1: id });

        if (r) {
            let q = ctx.AccountPermits as IQueryObject<AccountPermits>;
            q.Where(x => x.authObjectId == $args1 && x.authObjectType == $args2, { $args1: r.objectId, $args2: r.objectName });
            r.accountpermits = await q.ToList();

            r.object = await ctx[r.objectName].First(x => x.id == $args1, { $args1: r.objectId });

            let rp = await ctx.OrganizationTree.First(x => x.id == $args1, { $args1: r.parentId });
            if (rp) {
                r.parentObject = await ctx[rp.objectName].First(x => x.id == $args1, { $args1: rp.objectId });
            }
        }

        return r;
    }
    async QueryOrganizationTree(params) {
        let ctx = new ApiDataContext();
        let q = ctx.OrganizationTree as IQueryObject<OrganizationTree>;
        q = q.Where(x => x.isDelete == $args1, { $args1: false });
        if (params) {
            if (params.organizationId) {
                q = q.Where(x => x.organizationId == $args1, { $args1: params.organizationId });
            }
            if (params.objectName) {
                if (params.objectName instanceof Array) {
                    q = q.Contains(x => x.objectName, params.objectName) as IQueryObject<OrganizationTree>;
                }
                else {
                    q = q.Where(x => x.objectName == $args1, { $args1: params.objectName });
                }
            }
            if (params.objectId) {
                q = q.Where(x => x.objectId == $args1, { $args1: params.objectId });
            }
            if (params.parentId) {
                q = q.Where(x => x.parentId == $args1, { $args1: params.parentId });
            }
            if (params.objectIds) {
                q = q.Where(x => $args1.indexOf(x.objectId) > -1, { $args1: params.objectIds });
            }
        }
        let list = await q.ToList();

        return list;
    }
}